\doxysection{controller.\+h}
\hypertarget{controller_8h_source}{}\label{controller_8h_source}\index{C:/Users/ASUS/Desktop/dm-\/ctrlH7-\/balance-\/9025test/modules/algorithm/controller.h@{C:/Users/ASUS/Desktop/dm-\/ctrlH7-\/balance-\/9025test/modules/algorithm/controller.h}}
\mbox{\hyperlink{controller_8h}{Go to the documentation of this file.}}
\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ }
\DoxyCodeLine{00013\ \textcolor{preprocessor}{\#ifndef\ \_CONTROLLER\_H}}
\DoxyCodeLine{00014\ \textcolor{preprocessor}{\#define\ \_CONTROLLER\_H}}
\DoxyCodeLine{00015\ }
\DoxyCodeLine{00016\ \textcolor{preprocessor}{\#include\ "{}\mbox{\hyperlink{main_8h}{main.h}}"{}}}
\DoxyCodeLine{00017\ \textcolor{preprocessor}{\#include\ "{}stdint.h"{}}}
\DoxyCodeLine{00018\ \textcolor{preprocessor}{\#include\ "{}memory.h"{}}}
\DoxyCodeLine{00019\ \textcolor{preprocessor}{\#include\ "{}stdlib.h"{}}}
\DoxyCodeLine{00020\ \textcolor{preprocessor}{\#include\ "{}\mbox{\hyperlink{bsp__dwt_8h}{bsp\_dwt.h}}"{}}}
\DoxyCodeLine{00021\ \textcolor{preprocessor}{\#include\ "{}arm\_math.h"{}}}
\DoxyCodeLine{00022\ \textcolor{preprocessor}{\#include\ <math.h>}}
\DoxyCodeLine{00023\ }
\DoxyCodeLine{00024\ \textcolor{preprocessor}{\#ifndef\ abs}}
\DoxyCodeLine{00025\ \textcolor{preprocessor}{\#define\ abs(x)\ ((x\ >\ 0)\ ?\ x\ :\ -\/x)}}
\DoxyCodeLine{00026\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00027\ }
\DoxyCodeLine{00028\ \textcolor{comment}{//\ PID\ 优化环节使能标志位,通过位与可以判断启用的优化环节;也可以改成位域的形式}}
\DoxyCodeLine{00029\ \textcolor{keyword}{typedef}\ \textcolor{keyword}{enum}}
\DoxyCodeLine{00030\ \{}
\DoxyCodeLine{00031\ \ \ \ \ PID\_IMPROVE\_NONE\ =\ 0b00000000,\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ 0000\ 0000}}
\DoxyCodeLine{00032\ \ \ \ \ PID\_Integral\_Limit\ =\ 0b00000001,\ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ 0000\ 0001}}
\DoxyCodeLine{00033\ \ \ \ \ PID\_Derivative\_On\_Measurement\ =\ 0b00000010,\ \ \ \textcolor{comment}{//\ 0000\ 0010}}
\DoxyCodeLine{00034\ \ \ \ \ PID\_Trapezoid\_Intergral\ =\ 0b00000100,\ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ 0000\ 0100}}
\DoxyCodeLine{00035\ \ \ \ \ PID\_Proportional\_On\_Measurement\ =\ 0b00001000,\ \textcolor{comment}{//\ 0000\ 1000}}
\DoxyCodeLine{00036\ \ \ \ \ PID\_OutputFilter\ =\ 0b00010000,\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ 0001\ 0000}}
\DoxyCodeLine{00037\ \ \ \ \ PID\_ChangingIntegrationRate\ =\ 0b00100000,\ \ \ \ \ \textcolor{comment}{//\ 0010\ 0000}}
\DoxyCodeLine{00038\ \ \ \ \ PID\_DerivativeFilter\ =\ 0b01000000,\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ 0100\ 0000}}
\DoxyCodeLine{00039\ \ \ \ \ PID\_ErrorHandle\ =\ 0b10000000,\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ 1000\ 0000}}
\DoxyCodeLine{00040\ \}\ PID\_Improvement\_e;}
\DoxyCodeLine{00041\ }
\DoxyCodeLine{00042\ \textcolor{comment}{/*\ PID\ 报错类型枚举*/}}
\DoxyCodeLine{00043\ \textcolor{keyword}{typedef}\ \textcolor{keyword}{enum}\ errorType\_e}
\DoxyCodeLine{00044\ \{}
\DoxyCodeLine{00045\ \ \ \ \ PID\_ERROR\_NONE\ =\ 0x00U,}
\DoxyCodeLine{00046\ \ \ \ \ PID\_MOTOR\_BLOCKED\_ERROR\ =\ 0x01U}
\DoxyCodeLine{00047\ \}\ ErrorType\_e;}
\DoxyCodeLine{00048\ }
\DoxyCodeLine{00049\ \textcolor{keyword}{typedef}\ \textcolor{keyword}{struct}}
\DoxyCodeLine{00050\ \{}
\DoxyCodeLine{00051\ \ \ \ \ uint64\_t\ ERRORCount;}
\DoxyCodeLine{00052\ \ \ \ \ ErrorType\_e\ ERRORType;}
\DoxyCodeLine{00053\ \}\ \mbox{\hyperlink{struct_p_i_d___error_handler__t}{PID\_ErrorHandler\_t}};}
\DoxyCodeLine{00054\ }
\DoxyCodeLine{00055\ \textcolor{comment}{/*\ PID结构体\ */}}
\DoxyCodeLine{00056\ \textcolor{keyword}{typedef}\ \textcolor{keyword}{struct}}
\DoxyCodeLine{00057\ \{}
\DoxyCodeLine{00058\ \ \ \ \ \textcolor{comment}{//-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/\ init\ config\ block}}
\DoxyCodeLine{00059\ \ \ \ \ \textcolor{comment}{//\ config\ parameter}}
\DoxyCodeLine{00060\ \ \ \ \ \textcolor{keywordtype}{float}\ Kp;}
\DoxyCodeLine{00061\ \ \ \ \ \textcolor{keywordtype}{float}\ Ki;}
\DoxyCodeLine{00062\ \ \ \ \ \textcolor{keywordtype}{float}\ Kd;}
\DoxyCodeLine{00063\ \ \ \ \ \textcolor{keywordtype}{float}\ MaxOut;}
\DoxyCodeLine{00064\ \ \ \ \ \textcolor{keywordtype}{float}\ DeadBand;}
\DoxyCodeLine{00065\ }
\DoxyCodeLine{00066\ \ \ \ \ \textcolor{comment}{//\ improve\ parameter}}
\DoxyCodeLine{00067\ \ \ \ \ PID\_Improvement\_e\ Improve;}
\DoxyCodeLine{00068\ \ \ \ \ \textcolor{keywordtype}{float}\ IntegralLimit;\ \ \ \ \ \textcolor{comment}{//\ 积分限幅}}
\DoxyCodeLine{00069\ \ \ \ \ \textcolor{keywordtype}{float}\ CoefA;\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ 变速积分\ For\ Changing\ Integral}}
\DoxyCodeLine{00070\ \ \ \ \ \textcolor{keywordtype}{float}\ CoefB;\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ 变速积分\ ITerm\ =\ Err*((A-\/abs(err)+B)/A)\ \ when\ B<|err|<A+B}}
\DoxyCodeLine{00071\ \ \ \ \ \textcolor{keywordtype}{float}\ Output\_LPF\_RC;\ \ \ \ \ \textcolor{comment}{//\ 输出滤波器\ RC\ =\ 1/omegac}}
\DoxyCodeLine{00072\ \ \ \ \ \textcolor{keywordtype}{float}\ Derivative\_LPF\_RC;\ \textcolor{comment}{//\ 微分滤波器系数}}
\DoxyCodeLine{00073\ }
\DoxyCodeLine{00074\ \ \ \ \ \textcolor{comment}{//-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/}}
\DoxyCodeLine{00075\ \ \ \ \ \textcolor{comment}{//\ for\ calculating}}
\DoxyCodeLine{00076\ \ \ \ \ \textcolor{keywordtype}{float}\ Measure;}
\DoxyCodeLine{00077\ \ \ \ \ \textcolor{keywordtype}{float}\ Last\_Measure;}
\DoxyCodeLine{00078\ \ \ \ \ \textcolor{keywordtype}{float}\ Err;}
\DoxyCodeLine{00079\ \ \ \ \ \textcolor{keywordtype}{float}\ Last\_Err;}
\DoxyCodeLine{00080\ \ \ \ \ \textcolor{keywordtype}{float}\ Last\_ITerm;}
\DoxyCodeLine{00081\ }
\DoxyCodeLine{00082\ \ \ \ \ \textcolor{keywordtype}{float}\ Pout;}
\DoxyCodeLine{00083\ \ \ \ \ \textcolor{keywordtype}{float}\ Iout;}
\DoxyCodeLine{00084\ \ \ \ \ \textcolor{keywordtype}{float}\ Dout;}
\DoxyCodeLine{00085\ \ \ \ \ \textcolor{keywordtype}{float}\ ITerm;}
\DoxyCodeLine{00086\ }
\DoxyCodeLine{00087\ \ \ \ \ \textcolor{keywordtype}{float}\ Output;}
\DoxyCodeLine{00088\ \ \ \ \ \textcolor{keywordtype}{float}\ Last\_Output;}
\DoxyCodeLine{00089\ \ \ \ \ \textcolor{keywordtype}{float}\ Last\_Dout;}
\DoxyCodeLine{00090\ }
\DoxyCodeLine{00091\ \ \ \ \ \textcolor{keywordtype}{float}\ Ref;}
\DoxyCodeLine{00092\ }
\DoxyCodeLine{00093\ \ \ \ \ uint32\_t\ DWT\_CNT;}
\DoxyCodeLine{00094\ \ \ \ \ \textcolor{keywordtype}{float}\ dt;}
\DoxyCodeLine{00095\ }
\DoxyCodeLine{00096\ \ \ \ \ \mbox{\hyperlink{struct_p_i_d___error_handler__t}{PID\_ErrorHandler\_t}}\ ERRORHandler;}
\DoxyCodeLine{00097\ \}\ \mbox{\hyperlink{struct_p_i_d_instance}{PIDInstance}};}
\DoxyCodeLine{00098\ }
\DoxyCodeLine{00099\ \textcolor{comment}{/*\ 用于PID初始化的结构体*/}}
\DoxyCodeLine{00100\ \textcolor{keyword}{typedef}\ \textcolor{keyword}{struct\ }\textcolor{comment}{//\ config\ parameter}}
\DoxyCodeLine{00101\ \{}
\DoxyCodeLine{00102\ \ \ \ \ \textcolor{comment}{//\ basic\ parameter}}
\DoxyCodeLine{00103\ \ \ \ \ \textcolor{keywordtype}{float}\ Kp;}
\DoxyCodeLine{00104\ \ \ \ \ \textcolor{keywordtype}{float}\ Ki;}
\DoxyCodeLine{00105\ \ \ \ \ \textcolor{keywordtype}{float}\ Kd;}
\DoxyCodeLine{00106\ \ \ \ \ \textcolor{keywordtype}{float}\ MaxOut;\ \ \ \textcolor{comment}{//\ 输出限幅}}
\DoxyCodeLine{00107\ \ \ \ \ \textcolor{keywordtype}{float}\ DeadBand;\ \textcolor{comment}{//\ 死区}}
\DoxyCodeLine{00108\ }
\DoxyCodeLine{00109\ \ \ \ \ \textcolor{comment}{//\ improve\ parameter}}
\DoxyCodeLine{00110\ \ \ \ \ PID\_Improvement\_e\ Improve;}
\DoxyCodeLine{00111\ \ \ \ \ \textcolor{keywordtype}{float}\ IntegralLimit;\ \textcolor{comment}{//\ 积分限幅}}
\DoxyCodeLine{00112\ \ \ \ \ \textcolor{keywordtype}{float}\ CoefA;\ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ AB为变速积分参数,变速积分实际上就引入了积分分离}}
\DoxyCodeLine{00113\ \ \ \ \ \textcolor{keywordtype}{float}\ CoefB;\ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ ITerm\ =\ Err*((A-\/abs(err)+B)/A)\ \ when\ B<|err|<A+B}}
\DoxyCodeLine{00114\ \ \ \ \ \textcolor{keywordtype}{float}\ Output\_LPF\_RC;\ \textcolor{comment}{//\ RC\ =\ 1/omegac}}
\DoxyCodeLine{00115\ \ \ \ \ \textcolor{keywordtype}{float}\ Derivative\_LPF\_RC;}
\DoxyCodeLine{00116\ \}\ \mbox{\hyperlink{struct_p_i_d___init___config__s}{PID\_Init\_Config\_s}};}
\DoxyCodeLine{00117\ }
\DoxyCodeLine{00124\ \textcolor{keywordtype}{void}\ \mbox{\hyperlink{controller_8h_a2f151ba2f498df613fea5f497aeac704}{PIDInit}}(\mbox{\hyperlink{struct_p_i_d_instance}{PIDInstance}}\ *pid,\ \mbox{\hyperlink{struct_p_i_d___init___config__s}{PID\_Init\_Config\_s}}\ *config);}
\DoxyCodeLine{00125\ }
\DoxyCodeLine{00134\ \textcolor{keywordtype}{float}\ \mbox{\hyperlink{controller_8h_ac10c96e3f3e94b80d6df66c2c3ac53d0}{PIDCalculate}}(\mbox{\hyperlink{struct_p_i_d_instance}{PIDInstance}}\ *pid,\ \textcolor{keywordtype}{float}\ measure,\ \textcolor{keywordtype}{float}\ ref);}
\DoxyCodeLine{00135\ }
\DoxyCodeLine{00136\ \textcolor{preprocessor}{\#endif}}

\end{DoxyCode}
